<?php
import( 'de.ceus-media.framework.neon.DefinitionAction' );
import( 'classes.models.RightAction' );
/**
 *	Actions for Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
/**
 *	Actions for Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
class RightActionActions extends CMF_Krypton_DefinitionAction
{
	public function act()
	{
		$config		= $this->ref->get( "config" );
		$request	= $this->ref->get( "request" );

		$this->loadLanguage( 'right_action' );
		if( $request->get( 'rightaction_id' ) )
		{
			if( $request->get( 'editAction' ) )
				$this->editAction();
			else if( $request->get( 'removeAction' ) )
				$this->removeAction();
		}
		else if( $request->get( 'addAction' ) )
			$this->addAction();
	}

	/**
	 *	Adds a Right Action.
	 *	@access		private
	 *	@return		void
	 */
	private function addAction()
	{
		$request	= $this->ref->get( 'request' );
		if( $this->validateForm( 'right_action', 'addAction', 'right_action', 'add' ) )
		{
			$actionname	= $request->get( 'add_title' );
			$action	= new RightAction();
			$actions	= $action->getAllData();
			$actions	= $actions[$action->getTableName()];
			foreach( $actions as $action )
				if( strtolower( $action['title'] ) == strtolower( $actionname ) )
					return $this->messenger->noteError( $this->words['right_action']['msg']['error3'], $actionname );
			$action	= new RightAction();
			$data	= array(
				"title"		=> $actionname,
				"description"	=> $request->get( 'add_description' ),
				);
			$roid	= $action->addData( $data );
			$request->remove( 'add_action' );
			$request->remove( 'add_description' );
			$this->messenger->noteSuccess( $this->words['right_action']['msg']['success1'] );
		}
	}

	/**
	 *	Edits a Right Action.
	 *	@access		private
	 *	@return		void
	 */
	private function editAction()
	{
		$request	= $this->ref->get( 'request' );
		if( $rightaction_id = $request->get( 'rightaction_id' ) )
		{
			if( $this->validateForm( 'right_action', 'editAction', 'right_action', 'edit' ) )
			{
				$actionname = $request->get( 'edit_actionname' );
				$action	= new RightAction();
				$actions	= $action->getAllData();
				$actions	= $actions[$action->getTableName()];
				foreach( $actions as $action )
					if( $action['rightaction_id'] != $rightaction_id && strtolower( $action['title'] ) == strtolower( $actionname ) )
						return $this->messenger->noteError( $this->words['right_action']['msg']['error3'], $actionname );
				$data	= array(
					"title"		=> $actionname,
					"description"	=> $request->get( 'edit_description' ),
					);
				$action	= new RightAction( $rightaction_id );
				$action->modifyData( $data );
				if( $request->get( 'edit_action' ) == "add" )
					$request->remove( 'rightaction_id' );
				$this->messenger->noteSuccess( $this->words['right_action']['msg']['success2'], $actionname );
			}
		}
		else
			$this->messenger->noteError( $this->words['right_action']['msg']['error1'] );
	}

	/**
	 *	Removes a Right Action.
	 *	@access		private
	 *	@return		void
	 *	@todo		finish Implementation
	 */
	private function removeAction()
	{
		$request	= $this->ref->get( 'request' );
		if( $rightaction_id = $request->get( 'rightaction_id' ) )
		{
			$action	= new RightAction( $rightaction_id );
			$data	= $action->getData( false, true );
			$actionname	= $data['title'];
			$action->deleteData();
			$request->remove( 'rightaction_id' );
			$this->messenger->noteSuccess( $this->words['right_action']['msg']['success3'], $actionname );
		}
	}
}
?>